<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        padding: 0;
        margin: 0;
      }
      html,
      body {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        box-sizing: border-box;
        overflow: hidden;
      }
      .container {
        width: 100vw;
        padding: 20px;
        box-sizing: border-box;
      }
      .input {
        /* border: #ccc; */
        border: solid 1px #eee;
        outline: none;
        padding: 8px;
        font-size: 22px;
      }
      .done {
        text-decoration: line-through;
      }
      .lists {
        overflow: auto;
        max-height: 60vh;
        border: solid 1px #eee;
        margin: 10px auto;
        padding: 0 10px;
      }
      .list {
        display: flex;
        justify-content: space-between;
        align-items: center;
        border: solid 2px #e5e5e5;
        margin: 10px auto;
        padding: 10px 4px;
      }
      .delTodo {
        display: inline-block;
        box-sizing: border-box;
        margin-left: 4px;
        width: 24px;
        height: 24px;
        text-align: center;
        border-radius: 24px;
        cursor: pointer;
        background: #e00;
        color: #fff;
      }
    </style>
  </head>

  <body>
    <div id="app" class="container">
      <h1>Todo List</h1>
      <input class="input" placeholder="input" type="text" v-model="title" @keydown.enter="addTodo" />
      <ul class="lists">
        <li class="list" v-for="(todo,key) in todos">
          <p>
            <input type="checkbox" :id="todo.name" v-model="todo.done" />
            <label :for="todo.name" :class="{'done':todo.done}">{{todo.name}}</label>
          </p>
          <span class="delTodo" @click="deleteTodo(key)">x</span>
        </li>
      </ul>
      <div>
        <strong>完成率 </strong>
        <input type="checkbox" v-model="allDone" />
        <label> {{doned}}</label>
      </div>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/3.1.5/vue.global.min.js"></script>
    <script>
      let { ref, reactive, computed, toRefs } = Vue
      let App = {
        setup() {
          let title = ref('')
          let todos = reactive([
            { name: 'JavaScript', done: false },
            { name: 'Vue.js', done: true },
            { name: 'React.js', done: false },
          ])
          let addTodo = () => {
            todos.push({
              name: title.value,
              done: false,
            })
            title.value = ''
          }
          let deleteTodo = key => todos.splice(key, 1)
          // https://v3.cn.vuejs.org/api/computed-watch-api.html#computed
          let doned = computed(() => {
            return `${todos.filter(todo => todo.done).length} / ${todos.length}`
          })
          let allDone = computed({
            get: () => todos.filter(todo => !todo.done).length === 0,
            set: val => {
              todos.forEach(todo => (todo.done = val))
            },
          })
          return { title, todos, addTodo, doned, allDone, deleteTodo }
        },
      }
      Vue.createApp(App).mount('#app')
    </script>
  </body>
</html>
